package com.iflysse.services.impl;

import com.iflysse.dao.StudentDao;
import com.iflysse.dao.UserDao;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.*;
import com.iflysse.pojo.bo.resp.*;
import com.iflysse.pojo.po.common.CommonPoResp;
import com.iflysse.pojo.po.req.StudentHeadImagePoReq;
import com.iflysse.pojo.po.req.StudentPoReq;
import com.iflysse.pojo.po.req.TeacherClassIdPoReq;
import com.iflysse.pojo.po.resp.StudentPlainPoResp;
import com.iflysse.pojo.po.resp.StudentPoResp;
import com.iflysse.services.StudentService;
import com.iflysse.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentDao studentDao ;
    @Autowired
    UserDao userDao ;
    /**
     *  老师端添加学生时选择其中一个行政班级，下面的学生列表
     *  根据行政班级获取学生
     * @param classBoReq
     * @return
     */
    @Override
    public StudentPlainListBoResp getStudentPlainListByClass(ClassBoReq classBoReq) throws SQLException {
        //根据班级名称获取班级学生信息
        List<StudentPlainPoResp> studentPoRespList = studentDao.getStudentPlainListByClassName(classBoReq.getClassName());

        StudentPlainListBoResp studentPlainListBoResp = new StudentPlainListBoResp();
        //PO转BO,添加到集合中
        studentPoRespList.forEach(studentPlainPoResp -> {
            studentPlainListBoResp.getStudentPlainBoResps().add(CommonUtil.objToObj(studentPlainPoResp, StudentPlainBoResp.class));
        });
        return studentPlainListBoResp;
    }





    /**
     * 点击教学班级进来查看的教学班级学生的学生列表,
     *  老师端添加学生,选择其中一个教学班级,显示下面学生列表
     * 根据教学班级获取学生信息
     *
     * @param teachingClassIdBoReq
     * @return
     */
    @Override
    public StudentListBoResp getStudentListByTeacherClass(TeachingClassIdBoReq teachingClassIdBoReq) throws SQLException {
        TeacherClassIdPoReq teacherClassIdPoReq = CommonUtil.objToObj(teachingClassIdBoReq, TeacherClassIdPoReq.class);
        StudentListBoResp studentListBoResp = new StudentListBoResp();
        studentListBoResp.setStudentBoRespList(new ArrayList<>());
        //根据班级编号获取所有学生编号
        List<String> studentIdListByTeacherClass = studentDao.getStudentIdListByTeacherClass(teacherClassIdPoReq);
        if (studentIdListByTeacherClass.size() > 0) {
            //根据学生编号批量获取学生信息
            List<StudentPoResp> studentPoResps = studentDao.getStudentListByStudentIdList(studentIdListByTeacherClass);

            for(int i = 0;i<studentPoResps.size();i++){
                StudentBoResp studentBoResp = CommonUtil.objToObj(studentPoResps.get(i), StudentBoResp.class);

                studentBoResp.setLastLoginDate(userDao.getLastLoginDate(studentPoResps.get(i).getAccountId()));
                studentListBoResp.getStudentBoRespList().add(studentBoResp);
            }


        }
        return studentListBoResp;
    }

    /**
     * 根据学生编号获取学生信息
     * @param studentIdBoReq 学生编号
     * @return
     * @throws SQLException
     */
    @Override
    public StudentInfoBoResp getStudentInfoByStudentId(StudentIdBoReq studentIdBoReq) throws SQLException {
        StudentPoResp student = studentDao.getStudentByStudentId(studentIdBoReq.getStudentId());

        return CommonUtil.objToObj(student, StudentInfoBoResp.class);
    }

    /**
     * 更改学生头像
     *
     * @param studentHeadImageBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp updateStudentHeadImage(StudentHeadImageBoReq studentHeadImageBoReq) throws SQLException {
        StudentHeadImagePoReq studentHeadImagePoReq = CommonUtil.objToObj(studentHeadImageBoReq, StudentHeadImagePoReq.class);
        CommonPoResp commonPoResp =new CommonPoResp();
        int i = studentDao.updateHeadImage4Student(studentHeadImagePoReq);
        CommonUtil.printToPoResult(commonPoResp,i);
        return CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
    }

    /**
     * 更新学生信息
     *
     * @param studentBoReq 学生信息
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp updateStudentInfo(StudentBoReq studentBoReq) throws SQLException {
        StudentPoReq studentPoReq = CommonUtil.objToObj(studentBoReq, StudentPoReq.class);
        CommonPoResp commonPoResp =new CommonPoResp();
        int i = studentDao.updateStudent(studentPoReq);
        CommonUtil.printToPoResult(commonPoResp,i);
        return CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
    }


}
